


package yf.mall.controller;

import com.yf.request.SearchParam;

import java.io.IOException;
import java.util.List;

import com.yf.response.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.entity.UserEntity;
import yf.mall.service.UserService;
import yf.mall.vo.CouponVo;
import yf.mall.service.CouponService;
import com.yf.response.R;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

import org.springframework.web.multipart.MultipartFile;
import yf.mall.enums.CouponStatusEnum;


@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    /**
     * 列表卡券
     */
    @SysLog("查看卡券")
    @RequestMapping("/list")
    @RequiresPermissions("coupon:list")
    public R list(@RequestBody SearchParam<CouponVo> params) {
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<CouponVo> list = couponService.findVo(params.getEntity());
        return R.ok(new PageInfo(list));
    }

    /**
     * 下载
     */
    @SysLog("下载")
    @RequestMapping("/down")
    @RequiresPermissions("coupon:list")
    public void down(@RequestBody SearchParam<CouponVo> params, HttpServletResponse response) {
        List<CouponVo> list = couponService.findVo(params.getEntity());
        couponService.downLoad(response, list);
    }

    /**
     * 上传
     *
     * @param file
     */
    @RequestMapping("/upload")
    public void upload(MultipartFile file) {
        try {
            couponService.upload(file.getInputStream());
        } catch (IOException e) {
            throw new RRException("上传的文件有误!");
        }
    }


    /**
     * 查看卡券
     */
    @SysLog("查看卡券")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("coupon:info")
    public R info(@PathVariable("id") Long id) {
        CouponVo vo = couponService.getVo(id);

        return R.ok(vo);
    }

    /**
     * 保存卡券
     */
    @SysLog("保存卡券")
    @RequestMapping("/save")
    @RequiresPermissions("coupon:save")
    public R save(@RequestBody CouponVo vo) {
        UserEntity user = null;
        if(vo.getUser().getCode()!=null){
             user = userService.findByCode(vo.getUser().getCode());
        }
        if(user==null){
            throw new RRException("用户不存在");
        }
        vo.getEntity().setUserId(user.getId());
        couponService.saveVo(vo);
        return R.ok(vo);
    }

    @Autowired
    private UserService userService;

    /**
     * 修改卡券
     */
    @SysLog("修改卡券")
    @RequestMapping("/update")
    @RequiresPermissions("coupon:update")
    public R update(@RequestBody CouponVo vo) {
        couponService.updateVo(vo);

        return R.ok(vo);
    }

    /**
     * 删除卡券
     */
    @SysLog("删除卡券")
    @RequestMapping("/delete")
    @RequiresPermissions("coupon:delete")
    public R delete(Long[] ids) {
        couponService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有卡券
     */
    @SysLog("所有卡券")
    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order != null ? order : "id desc");
        List<CouponVo> list = couponService.findVo(null);
        return R.ok(list);
    }

    /**
     * 所有enums
     */
    @SysLog("所有enums")
    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String, Object> options = new HashMap();
        options.put("statusOptions", CouponStatusEnum.names());
        return R.ok(options);
    }


    /***********自定义开始***********/
}
